<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class ActiveRecord extends CActiveRecord
{
    public function __construct($scenario='insert') {
        parent::__construct($scenario);
    }
    
	public function setData($data = array())
	{
		$keys = array_keys($data);
        foreach($keys as $key){
			$this -> {$key} = $data[$key];          
		}
	}
	
    public function executeAll($query, $params=array(), $isPaging=true)
    {
        if($isPaging)
        {
            $start = 0;
            $limit = Yii::app()->params['recordsPerPage'];
            
            if(isset($_GET['skip']))
            {
                $start = $_GET['skip'] + 0;
            }
            
            if(isset($_GET['pageSize']))
            {
                $limit = $_GET['pageSize'] + 0;
            }
            
            $query .= " LIMIT :start, :limit";
            
            $params[':start'] = $start;
            $params[':limit'] = $limit;
        }
        
        $command = Yii::app() -> db -> createCommand($query);
        $command ->bindValues($params);
        
        $result = $command ->queryAll() ;
        
        return $result; 
    }
    
    public function executeAllTotal($query, $params=array(), $isPaging=true, $sortDefault = "", $start = 0, $limit = 25)
    {        
        $orderBy = "";
        if(isset($_POST['sort']))
        {
            $sort = $_POST['sort'];
            $orderBy = " order by " . 't.' . $sort[0]['field'] . " " . $sort[0]['dir'];
            
            $query .= $orderBy;
        }
        else
		{
			if($sortDefault != "")
			{
				$query .= "order by " . $sortDefault;
			}
		}
		
        if($isPaging)
        {   
            /*
            if(isset(Yii::app()->params['recordsPerPage']))
                $limit = Yii::app()->params['recordsPerPage'];
            */
            if(!stristr($query, 'sql_calc_found_rows'))
            {
                $temp = preg_split('/^\w*\s/', $query);
                $query = "SELECT sql_calc_found_rows " . $temp[1];  
            }
            
            if(isset($_POST['skip']))
            {
                $start = $_POST['skip'] + 0;
            }
            
            if(isset($_POST['pageSize']))
            {
                $limit = $_POST['pageSize'] + 0;
            }
            
            $query .= " LIMIT :start, :limit";
            
            $params[':start'] = $start;
            $params[':limit'] = $limit;
            
        }
        
        $command = Yii::app() -> db -> createCommand($query);
        $command ->bindValues($params);
        
        $result = $command ->queryAll();
        
        for($i = 0; $i < count($result); $i++)
        {
        	$result[$i]["grid_index"] = $i + 1;
        }
        
        $query = "SELECT found_rows()";
        $command = Yii::app() -> db -> createCommand($query);
        
        $total = $command ->queryScalar();
        
        return array($result, $total); 
    }
          
    public function executeScalar($query, $params=array())
    {
        $command = Yii::app() -> db -> createCommand($query);
        $command ->bindValues($params);
        $result = $command ->queryScalar() ;
        
        return $result; 
    }
    
    public function executeRow($query, $params=array())
    {
        $command = Yii::app() -> db -> createCommand($query);
        $command ->bindValues($params);
        $result = $command ->queryRow() ;
        
        return $result; 
    }
    
    public function executeNone($query, $params=array())
    {
        $command = Yii::app() -> db -> createCommand($query);
        $command ->bindValues($params);
        $result = $command ->execute();
        
        return $result; 
    }
}